<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
/**
 * this is meant to allow for the generation of events
 * 
 * @todo make this a full mock object dropin replacemnet
 *
 */
class X_Broker_Event_Mock
{
    /**
     * full path of the current running script
     * including appended path
     * 
     * Answers "Where are we currently at in
     * relation to domain root?"
     *
     * @var string
     */
    public static $sFullPath;
    /**
	 * current query string
	 *
	 * @var string
	 */
    public static $sUrlQuery;
    /**
     * base url with appended path removed
     * 
     * Answers "Where is the controller?"
     *
     * @var string
     */
    public static $sBaseUrl;
    /**
     * array of path crums to be processed
     *
     * @var array
     */
    protected static $aPath;
    /**
     * array of processed path crums
     *
     * @var array
     */
    protected static $aHistory;
    /**
     * array of submited data
     *
     * @var array
     */
    protected static $aData;
    
    /**
     * @return array
     */
    public static function getData ()
    {
        if (isset(self::$aData))
        {
            return self::$aData;
        }
        
        /**
         * @todo add filtering to GPR data
         */
        self::$aData = array_merge($_GET, $_POST);
        
        return self::$aData;
    }
    /**
     * @param array $aData
     */
    public static function setData ($aData = null)
    {
        self::$aData = $aData;
    }
    /**
     * @return array
     */
    public static function getHistory ()
    {
        return self::$aHistory;
    }
  
    /**
     * returns the path beyond the controller, and returns 
     * the crumbs in an array
     * 
     * @param bool $bRefresh force refresh
     * @return array
     */
    public static function getPath ($bRefresh = false)
    {
        if (self::$aPath && !$bRefresh)
        {
            return self::$aPath;
        }
        
        $sPathInfo = trim(self::getPathInfo());
        
        if (empty($sPathInfo))
        {
            self::$aPath = array('index', 'index');
        }
        else
        {
            self::$aPath = explode('/', $sPathInfo);
            if (empty(self::$aPath[0]))
            {
                unset(self::$aPath[0]);
            }
        }
        
        if (count(self::$aPath) == 1)
        {
            array_push(self::$aPath, 'index');
        }
        
        return self::$aPath;
    }
    /**
     * @param array $aPath
     */
    public static function setPath ($xPath = null)
    {
        if (empty($xPath))
        {
            // if we passed nothing in, we need to set it
            self::getPath(true);            
        }
        else if (is_array($xPath))
        {
            self::$aPath = $xPath;
        }
        else
        {
            self::$aPath = explode('/', (string)$xPath);
        }
    }
    
    /**
     * @return string
     */
    public static function getBaseURL ()
    {
        return self::$sBaseUrl;
    }
    /**
     * @param string $sBaseUrl
     */
    public static function setBaseURL ($sBaseURL = null)
    {
        self::$sBaseUrl = $sBaseURL;
    }
    /**
     * @return string
     */
    public static function getFullPath ()
    {
        return self::$sFullPath;
    }
    /**
     * @param string $sFullPath
     */
    public static function setFullPath ($sFullPath)
    {
        self::$sFullPath = $sFullPath;
    }
    
    public static function getInstance ()
    {
        $sMyClass = __CLASS__;
        $oInstance = new $sMyClass();
        return $oInstance;
    }
    
	/**
	 * obtains the path from the $_ENV or $_SERVER predefined arrays
	 *
	 * @return string|false if not set in either array
	 */
	public static function getPathInfo()
	{
	    $sPathInfo = '';

	    if (array_key_exists('PATH_INFO', $_ENV) && !empty($_ENV['PATH_INFO']))
		{
			$sPathInfo = $_ENV['PATH_INFO'];
		}
		elseif (array_key_exists('PATH_INFO', $_SERVER) && !empty($_SERVER['PATH_INFO']))
		{
			$sPathInfo = $_SERVER['PATH_INFO'];
		}
		elseif (array_key_exists('SCRIPT_NAME', $_SERVER))
		{
		    if (array_key_exists('REQUEST_URI', $_SERVER))
		    {
		        self::$sFullPath = parse_url($_SERVER['REQUEST_URI']);
		    }
		    else
		    {
		        self::$sFullPath = parse_url($_SERVER['SCRIPT_URL']);
		    }
		    
		    if (!empty(self::$sFullPath['query']))
    		{
    		    self::$sUrlQuery = self::$sFullPath['query'];
    		}
    		
    		self::$sFullPath = self::$sFullPath['path'];
		    
		    $sPathInfo = str_replace(dirname($_SERVER["SCRIPT_NAME"]), '', self::$sFullPath);
		    $sPathInfo = str_replace('//', '/', str_replace(basename($_SERVER["SCRIPT_NAME"]), '', $sPathInfo));
		    
		    self::$sBaseUrl = parse_url(str_replace($sPathInfo, '',$_SERVER['PHP_SELF']));
		    self::$sBaseUrl = self::$sBaseUrl['path'];
		}
		
		if (strlen(trim($sPathInfo)) > 1)
		{
		    return $sPathInfo;
		}

		return false;
	}
	
	function __construct()
	{
	    self::getPath();
	    self::getData();
	}

}